package com.likersai.opon.scene.cultivation.service.impl;

import com.likersai.opon.pojo.bo.PageBO;
import com.likersai.opon.pojo.bo.PageParam;
import com.likersai.opon.scene.cultivation.dao.ConfigRealmDao;
import com.likersai.opon.scene.cultivation.entity.ConfigRealm;
import com.likersai.opon.scene.cultivation.service.ConfigRealmService;
import com.likersai.opon.scene.cultivation.service.req.MoveConfigRealmReq;
import com.likersai.opon.scene.cultivation.service.req.QueryConfigRealmReq;
import com.likersai.opon.scene.cultivation.service.req.SaveConfigRealmReq;
import com.likersai.opon.scene.cultivation.service.rsp.ConfigRealmRsp;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 配置表-境界(ConfigRealm)表服务实现类
 *
 * @author sun.ye
 * @since 2024-10-07 01:34:33
 */
@RequiredArgsConstructor
@Service("configRealmService")
public class ConfigRealmServiceImpl implements ConfigRealmService {
    
    
    private final ConfigRealmDao configRealmDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ConfigRealmRsp query(Long id) {
        ConfigRealm entity = configRealmDao.selectOne(id);  
        return ConfigRealmRsp.from(entity);
    }

    /**
     * 分页查询
     *
     * @param req 筛选条件
     * @return 查询结果
     */
    @Override
    public PageBO<ConfigRealmRsp> page(QueryConfigRealmReq req) {
        PageParam page = req.toPageParam();
        int total = configRealmDao.count(req.toParam());
        List<ConfigRealm> list = configRealmDao.list(req.toParam(),page.getRowStart(),page.getOffset());
        return PageBO.getPage(ConfigRealmRsp.from(list),total);
    }

    /**
     * 保存数据
     *
     * @param saveReq 入参
     * @return 实例对象
     */
    @Override
    public ConfigRealm save(SaveConfigRealmReq saveReq) {
        ConfigRealm entity = saveReq.toEntity();
        
        if(saveReq.getId() == null){
            configRealmDao.insert(entity);
        }else {
            configRealmDao.update(entity);
        }
     
        return configRealmDao.selectOne(entity.getId());
    }

    /**
     * 删除数据
     *
     * @param moveReq 入参
     * @return 实例对象
     */
    @Override
    public boolean moveById(MoveConfigRealmReq moveReq) {
        return configRealmDao.deleteById(moveReq.getId()) > 0;
    }

}
